package com.sooncode.project.focusservice.service;

import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.core.finder.Page;
import com.sooncode.project.core.model.DomainException;
import com.sooncode.project.focusservice.model.Fee;
import com.sooncode.project.focusservice.model.Organization;
import com.sooncode.project.springcloudcommon.model.AuthInfo;
import com.sooncode.project.springcloudcommon.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class FeeService {
    public boolean add(Map map){
        Fee fee=new Fee();
        fee.add(map);
        return true;
    }
    public boolean modify(String id,Map map){
        Fee fee=getById(id);
        fee.modify(map);
        return true;
    }
    public boolean delete(String id){
        Fee fee=getById(id);
        fee.delete();
        return true;
    }
    public Page<Fee> getPageList(String memberId,int pageSize, int pageIndex, Map map) {
        if(StringUtils.isEmpty(memberId))
            throw new DomainException("会员id不能为空");
        return new Finder<>(Fee.class)
                .byField("memberId",memberId).and(map).page(pageSize, pageIndex);
    }
    public Fee getById(String id){
        return new Finder<>(Fee.class).byId(id);
    }

    public Page<Fee> getPageListForDevice(int pageSize, int i, Map map) {
        AuthInfo authInfo= UserUtils.getCurrentUser();
        Organization org=new Finder<>(Organization.class)
                .byField("accountId",authInfo.getUserid())
                .first();
        return new Finder<>(Fee.class)
                .byField("orgId",org.getId())
                .and(map).page(pageSize, i);
    }

    public boolean addForDevice(Map map) {
        AuthInfo authInfo= UserUtils.getCurrentUser();
        Organization org=new Finder<>(Organization.class)
                .byField("accountId",authInfo.getUserid())
                .first();
        map.put("orgId",org.getId());
        return add(map);
    }

    public boolean modifyForDevice(String id, Map map) {
        AuthInfo authInfo= UserUtils.getCurrentUser();
        Organization org=new Finder<>(Organization.class)
                .byField("accountId",authInfo.getUserid())
                .first();
        map.put("orgId",org.getId());
        return modify(id,map);
    }
}
